#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <event2/event.h>
#include <string.h>
void write_cb(evutil_socket_t fd, short what, void* arg)
{
        printf("write event: %s\n", what & EV_WRITE?"Yes":"No");
        char buf[BUFSIZ] = {0};
        static int num = 0;
        if(what & EV_WRITE){
                sprintf(buf, "hello, world == %d\n",num++);
                write(fd,buf,strlen(buf)+1);
        }
}
//  写管道
int main(int argc, char const *argv[])
{
        int fd = open("myfifo",O_WRONLY  | O_NONBLOCK);
        if(fd == -1){
                perror("open error");
                exit(1);
        }

        //写管道
        struct event_base* base = NULL;
        base = event_base_new();

        //创建事件
        struct event* ev = NULL;
        // 检测是写缓冲区是否有空间去写
        ev = event_new(base, fd, EV_WRITE | EV_PERSIST,write_cb,base);

        //添加事件
        event_add(ev, NULL);

        //事件循环
        event_base_dispatch(base);

        //释放资源
        event_free(ev);
        event_base_free(base);
        close(fd);
        return 0;
}
